package ai.people.core.nebula.util;

import ai.people.netmon.framework.constant.GraphConstant;
import ai.people.netmon.framework.domain.graph.enums.GraphTagEnum;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhenglin
 * @date 2022/5/13
 */
public class VidGenerateUtils {

    /**
     * 处理特殊情况
     *
     * @param propertiesValues 属性值
     * @param byTagName        通过标签名称
     * @param archiveId        归档id
     * @return {@link Object[]}
     */
    public static List<Object> handleSpecialVid(Map<String, Object> propertiesValues, GraphTagEnum byTagName, String archiveId) {
        //需要多个属性才能生成vid的情况
        int size = byTagName.vidPropertiesKeys().size();
        List<Object> vidValues = new ArrayList<>();
        boolean needAddArchiveId = false;
        for (int i = 0; i < size; i++) {
            String key = byTagName.vidPropertiesKeys().get(i);
            if (key.startsWith(GraphConstant.MD5)) {
                //如果vid需要加密
                key = key.replaceAll(GraphConstant.MD5, "");
                vidValues.add(DigestUtils.md5Hex(propertiesValues.get(key).toString()));
            } else if (key.startsWith(GraphConstant.BASE)) {
                //如果vid需要加上档案ID
                key = key.replaceAll(GraphConstant.BASE, "");
                vidValues.add(propertiesValues.get(key).toString());
                needAddArchiveId = true;
            } else {
                vidValues.add(propertiesValues.get(key));
            }
        }
        if (needAddArchiveId && StringUtils.isNotBlank(archiveId)) {
            vidValues.add(0, archiveId);
        }
        return vidValues;
    }


}
